using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Perovich.GameObjects.Interfaces;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Perovich.GameObjects.Helpers;

namespace Perovich.GameObjects.TwoDimensional.Particles
{
    internal class ParticleManager : GameObject
    {



        public static ParticleManager Instance { get; private set; }

        public static ParticleManager Initialize(Interfaces.ICanHoldComponents game)
        {
            if (Instance == null)
            {
                Instance = new ParticleManager(game);

                Perovich.GameObjects.ComponentsExtensions.Helpers.Add(new Helper(typeof(ParticleManager).GetMethod("Reset", System.Reflection.BindingFlags.Static)));
            }
            return Instance;
        }

        public static void Reset()
        {
            Instance = null;
        }

        public ParticleManager(Interfaces.ICanHoldComponents game)
            : base(game)
        {
            BackgroundColor = Color.Transparent;
            Width = 10;
            Height = 10;
        }


        protected override void LoadContent()
        {
            base.LoadContent();
        }


        public override void DrawOnTargets(GameTime gameTime)
        {
            RenderTarget2D t;
            t = RenderTargetManager.GetRenderTarget(GraphicsDevice, Width, Height, this);
            GraphicsDevice.SetRenderTarget(t);
            GraphicsDevice.Clear(BackgroundColor);
            //SBatches.Begin(SpriteSortMode.BackToFront, BlendState.Additive);
            base.Draw(gameTime);
            //SBatches.End();
            GraphicsDevice.SetRenderTarget(null);
            base.DrawOnTargets(gameTime);
        }

        public override void Draw(Microsoft.Xna.Framework.GameTime gameTime)
        {
            SBatches.CurrentBatch.Draw(RenderTargetManager.GetRenderTarget(GraphicsDevice, Width, Height, this), new Rectangle(0, 0, (int)Width, (int)Height), new Rectangle(0, 0, (int)Width, (int)Height), Color.White, 0, Vector2.Zero, SpriteEffects.None, 0);
        }


        public Color BackgroundColor { get; set; }

        public float Width { get; set; }

        public float Height { get; set; }
    }
}
